//
// ant.cpp
//  - the ant object
//

#include <stdio.h>
#include <stdlib.h>

#include <GL/gl.h>			// Header File For The OpenGL32 Library
#include <GL/glu.h>			// Header File For The GLu32 Library


#include "gldrawlib.h"
#include "objects.h"
#include "lights.h"

#undef CURRENT_OBJECT
#define CURRENT_OBJECT		norm_cube

static void init_norm_cube(int list_id);
static void compile_norm_cube(void);
static void draw_norm_cube(void);
static void render_norm_cube(void);
static void draw_norm_cube(void);



//
// simple objects library
// - make sure to change the number of objects
// in objects.h
//
DriverObjects CURRENT_OBJECT =
{
	init_norm_cube,			// init, must be called first
	compile_norm_cube,		// compile
	draw_norm_cube,			// draw 
	render_norm_cube,			// render to scene
	0					// loaded by INIT
};


//=========================================================
//=========================================================
//=========================================================
// draw cube with normals turned on
// Note: have to use triangles, (dope!)
// - also no particular order when drawing triangles
//=========================================================
static void draw_norm_cube(void)
{
	float v[3][3] = { 0 };
	float n[3] = { 0 };

	float size = 1.0f;

	
	// Note: normals are messed up for now
	// select between n0-n3

	// change the size here
	// Note: starts from ground

	glBegin(GL_TRIANGLES);
	 
	// left bottom front
	  v[0][0] = -size;
	  v[0][1] = 0.0f;
	  v[0][2] = size;

	  v[1][0] = size;
	  v[1][1] = 0.0f;
	  v[1][2] = size;

	  v[2][0] = size;
	  v[2][1] = size;
	  v[2][2] = size;


	  CLR_0;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;

	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom front

	  // Finish the front
	  v[0][0] = size;
	  v[0][1] = size;
	  v[0][2] = size;

	  v[1][0] = -size;
	  v[1][1] = size;
	  v[1][2] = size;

	  v[2][0] = -size;
	  v[2][1] = 0.0f;
	  v[2][2] = size;

	  CLR_1;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;

	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom front

	  // Draw the back triangle
	  //-----------------------------
	  v[0][0] = -size;
	  v[0][1] = 0.0f;
	  v[0][2] = -size;

	  v[1][0] = size;
	  v[1][1] = 0.0f;
	  v[1][2] = -size;

	  v[2][0] = size;
	  v[2][1] = size;
	  v[2][2] = -size;

	  CLR_2;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;

	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom bac

	  // Finish the back
	  v[0][0] = size;
	  v[0][1] = size;
	  v[0][2] = -size;

	  v[1][0] = -size;
	  v[1][1] = size;
	  v[1][2] = -size;

	  v[2][0] = -size;
	  v[2][1] = 0.0f;
	  v[2][2] = -size;

	  MED_PURPLE;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom front

	  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?
	  // Draw the right side
	  // Triangle
	  v[0][0] = size;
	  v[0][1] = 0.0f;
	  v[0][2] = size;

	  v[1][0] = size;
	  v[1][1] = 0.0f;
	  v[1][2] = -size;

	  v[2][0] = size;
	  v[2][1] = size;
	  v[2][2] = size;

	  MED_BLUE;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;

	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom bac

	  // FINISh the right side of the box
	  v[0][0] = size;
	  v[0][1] = 0.0f;
	  v[0][2] = -size;

	  v[1][0] = size;
	  v[1][1] = size;
	  v[1][2] = -size;

	  v[2][0] = size;
	  v[2][1] = size;
	  v[2][2] = size;

	  MED_GREEN;
	  // Calc normal and draw
	  	  N_2;
		  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom bac

	  // FINISh the left side of the box
	  v[0][0] = -size;
	  v[0][1] = 0.0f;
	  v[0][2] = -size;

	  v[1][0] = -size;
	  v[1][1] = size;
	  v[1][2] = -size;

	  v[2][0] = -size;
	  v[2][1] = size;
	  v[2][2] = size;


	  MED_PURPLE;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left bottom bac

	  // Draw the left side
	  // Triangle
	  v[0][0] = -size;
	  v[0][1] = 0.0f;
	  v[0][2] = size;

	  v[1][0] = -size;
	  v[1][1] = 0.0f;
	  v[1][2] = -size;

	  v[2][0] = -size;
	  v[2][1] = size;
	  v[2][2] = size;

	  MED_RED;
	  // Calc normal and draw
	 	  N_2;
		  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left side

	  // Draw the top and bottom
	  v[0][0] = size;
	  v[0][1] = size;
	  v[0][2] = size;

	  v[1][0] = size;
	  v[1][1] = size;
	  v[1][2] = -size;

	  v[2][0] = -size;
	  v[2][1] = size;
	  v[2][2] = -size;

	  CLR_0;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left side

	  // Draw one of the bottom triangles
	  v[0][0] = size;
	  v[0][1] = 0.0f;
	  v[0][2] = size;

	  v[1][0] = size;
	  v[1][1] = 0.0f;
	  v[1][2] = -size;

	  v[2][0] = -size;
	  v[2][1] = 0.0f;
	  v[2][2] = -size;


	  CLR_3;
	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left side

	  // Lets finish the bottom with the second triangle
	  v[0][0] = -size;
	  v[0][1] = 0.0f;
	  v[0][2] = size;

	  v[1][0] = size;
	  v[1][1] = 0.0f;
	  v[1][2] = size;

	  v[2][0] = -size;
	  v[2][1] = 0.0f;
	  v[2][2] = -size;

	  // Calc normal and draw
	 	  N_2;
		  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left side

	  // Go back and finish the top
	  v[0][0] = -size;
	  v[0][1] = size;
	  v[0][2] = size;

	  v[1][0] = size;
	  v[1][1] = size;
	  v[1][2] = size;

	  v[2][0] = -size;
	  v[2][1] = size;
	  v[2][2] = -size;

	  // Calc normal and draw
	  N_2;
	  GET_NORMAL;
	  glVertex3fv(v[0]);
	  glVertex3fv(v[1]);
	  glVertex3fv(v[2]);	// triangle left side

	glEnd();

} // end of the function


//
// init
// - load anything special about the
// one important function 
//
static void init_norm_cube(int list_id)
{

	CURRENT_OBJECT.visible = 1;

	// store the id through the function
	// there is probably a better way to do this
	CURRENT_OBJECT.call_id = list_id;	
	
} // end of the functino


//=========================================================
// Now the function to actually draw it
//=========================================================
static void render_norm_cube(void)
{
		//glPushMatrix();

			glCallList(CURRENT_OBJECT.call_id);

		//glPopMatrix();

} // end of the function

//=========================================================
// compile
//=========================================================
static void compile_norm_cube(void)
{
	int id;
	// setup a spot for display list for background
	//object = getcurrentobject();
	id = CURRENT_OBJECT.call_id;

	// apply list
	glNewList(id, GL_COMPILE);

		// call drawing function
		// but this may method make it a little better
		CURRENT_OBJECT.draw();

	glEndList();

} // end of the function

